import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [

  {
    path: '/',
    name: 'Manager',
    component: () => import('../views/Manager.vue'),
    children: [
      {path: 'user', name:'User', component: () => ('../views/manager/User.vue')},
      {path: 'home', name:'Home', component: () => ('../views/manager/Home.vue')},
      {path: '403', name: 'Auth', component: () => import('../views/manager/Auth.vue')}
    ]
  },
  {path: '/element', name: 'Element', component: () => import('../views/Element.vue'),},
  {path: '/login', name: 'Login', component: () => import('../views/Login.vue')},
  {path: '/register', name: 'Register', component: () => import('../views/Register.vue')},
  {path: '*', name: 'notFound', component: () => import('../views/notFound.vue')},

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach( (to, from, next) => {
  let adminPath = ['/manger/user']
  let user = JSON.parse(localStorage.getItem('user_data')||'{}')
  if (to.path === '/403') {
    next()
    return
  }
  if(user.role !=='管理员'&& adminPath.includes(to.path)){
    next('/403')
  }else {
    next()
  }
})

export default router
